package com.zyj.myapplication.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zyj.myapplication.Javabean.User;
import com.zyj.myapplication.Model.UserModel;
import com.zyj.myapplication.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.zyj.myapplication.R.id.username;

public class RegisterActivity extends AppCompatActivity {
    private EditText telephone,password1,passwordagain;
    private Button btn_reg,btn_return;
    private String error;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();//初始化
        initEvents();//初始化事件
    }
    private void initViews(){
        telephone=(EditText) findViewById(R.id.username1);
        password1=(EditText) findViewById(R.id.password1);
        passwordagain=(EditText) findViewById(R.id.passwordagain);
        btn_reg=(Button) findViewById(R.id.button4);
        btn_return=(Button) findViewById(R.id.button5);
    }
    private void initEvents(){
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent1);
            }
        });
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str_tel=telephone.getText().toString();
                String str_pwd=password1.getText().toString();
                String str_pwdagain=passwordagain.getText().toString();


                if(str_tel.equals("") || str_pwd.equals("")){
                    Toast.makeText(RegisterActivity.this,"您有部分信息还未填写",Toast.LENGTH_SHORT).show();
                } else{
                    if(str_pwd.equals(str_pwdagain)){
                        UserModel userModel = new UserModel();
                        Call<User> call = userModel.register(str_tel,str_pwd);
                        //发送请求
                        call.enqueue(new Callback<User>() {
                            @Override
                            public void onResponse(Call<User> call, Response<User> response) {
                                error = response.body().getError();
                                if(!"手机号已经被使用".equals(error)){
                                    Toast.makeText(getApplication(),"注册成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent();
                                    String str_user=telephone.getText().toString();
                                    intent.putExtra("username",str_user);
                                    setResult(RESULT_OK,intent);
                                    finish();
                                } else{
                                    Toast.makeText(RegisterActivity.this, "该手机号已经被注册", Toast.LENGTH_SHORT).show();
                                    }

                            }

                            @Override
                            public void onFailure(Call<User> call, Throwable t) {
                                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();

                            }
                        });
                    } else{
                        Toast.makeText(RegisterActivity.this, "两次密码输入不同，请重新确认",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }




























}
